﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using ERP.Domain.SalesManagement;
using Sell.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sell.Services.Contracts
{
    public class ContractServices : IContractServices
    {
        public IContractsRepository repository { get; }
        public ContractServices(IContractsRepository _repository)
        {
            repository = _repository;
        }
        /// <summary>
        /// 采购合同添加
        /// </summary>
        /// <param name="contract"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddContract(Contract contract)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "采购合同添加成功";
            var list = await repository.AddAsync(contract);
            res.Data = list;
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 删除采购合同
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteContract(int id)
        {
            ApiResult<int> res = new ApiResult<int>();
            try
            {
                //根据ID获取客户信息
                var con = await repository.GetModelAsync(id);
                if (con == null)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "采购合同不存在";
                    return res;
                }
                //将IsDelete状态设置为true
                con.IsDelete = true;
                //更新客户信息
                var result = await repository.Update(con);
                res.Code = EnumCode.Ok;
                res.Message = "采购合同删除成功";
                res.Data = result;

            }
            catch (Exception ex)
            {
                res.Code = EnumCode.Fail;
                res.Message = $"删除采购合同失败:{ex.Message}";
            }
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 修改采购合同
        /// </summary>
        /// <param name="contract"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateContract(Contract contract)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "采购合同修改成功";
            var list = await repository.Update(contract);
            res.Data = list;
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 修改审核状态
        /// </summary>
        /// <param name="id">合同ID</param>
        /// <param name="reviewStatus">审核状态 (0-未审核, 1-已审核)</param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateReviewStatus(int id, int reviewStatus)
        {
            ApiResult<int> res = new ApiResult<int>();
            try
            {
                // 根据ID获取合同信息
                var contract = await repository.GetModelAsync(id);
                if (contract == null)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "采购合同不存在";
                    return res;
                }

                // 验证审核状态值是否有效
                if (reviewStatus < 0 || reviewStatus > 1)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "审核状态值无效，有效值：0-未审核, 1-已审核";
                    return res;
                }

                // 更新审核状态
                contract.Review = reviewStatus;
                var result = await repository.Update(contract);
                
                res.Code = EnumCode.Ok;
                res.Message = reviewStatus == 0 ? "未审核" : "已审核";
                res.Data = result;
            }
            catch (Exception ex)
            {
                res.Code = EnumCode.Fail;
                res.Message = $"修改审核状态失败: {ex.Message}";
            }
            return await Task.FromResult(res);
        }
    }
}
